import { createRouter, createWebHashHistory } from 'vue-router'
import { ref } from 'vue'
import { Session } from '../services/session'

const router = createRouter({
    history: createWebHashHistory(),
    routes: [
        {
            path: '/', replace: true, component: () => import('~/views/Cashier.vue'),
            meta: {
                hide: false, title: '首页', icon: 'ui-ico-cashier',
                prod: ['base', 'pro'], roles: [1,2, 3],noscroll:true
            }
        },
        {
            path: '/goods', replace: true, component: () => import('~/views/Main.vue'),
            meta: {
                hide: false, title: '商品目录', icon: 'ui-ico-order',
                prod: ['base', 'pro'], roles: [1,2,3],noscroll:true
            }
        },
        {
            path: '/store', replace: true, component: () => import('~/views/Store.vue'),
            meta: {
                hide: false, title: '库存管理', icon: 'ui-ico-admin',
                prod: ['base', 'pro'], roles: [1, 2, 3],noscroll:true
            }
        },
        {
            path: '/orders', component: () => import('~/views/Order.vue'),
            meta: {
                hide: false, title: '报表', icon: 'ui-ico-order',
                prod: ['base', 'pro'], roles: [1, 2, 3], noscroll:true
            },
            children: [
                {
                    path: '/order/exchange', component: () => import('~/views/order/Exchange.vue'),
                    meta: {
                        hide: false, title: '交班明细', icon: 'ui-ico-handshake',
                        prod: ['base', 'pro'], roles: [1, 2, 3]
                    }
                },
                {
                    path: '/order/documents', component: () => import('~/views/order/Documents.vue'),
                    meta: {
                        hide: false, title: '单据查询', icon: 'ui-ico-doc',
                        prod: ['base', 'pro'], roles: [1, 2, 3]
                    }
                },
                {
                    path: '/order/commonorders', component: () => import('~/views/order/CommonOrders.vue'),
                    meta: {
                        hide: false, title: '订单记录', icon: 'ui-ico-order',
                        prod: ['base', 'pro'], roles: [1, 2, 3]
                    }
                },
                {
                    path: '/order/cashout', component: () => import('~/views/order/Cashout.vue'),
                    meta: {
                        hide: false, title: '提现记录', icon: 'ui-ico-cashout',
                        prod: ['base', 'pro'], roles: [1, 2, 3]
                    }
                }
            ]
        },
        {
            path: '/admin', component: () => import('~/views/Admin.vue'),
            meta: {
                hide: false, title: '管理', icon: 'ui-ico-settings',
                roles: [1, 2, 3], prod: ['base', 'pro', 'super']
            },
            children: [
                {
                    path: '/admin/netbar', component: () => import('~/views/admin/NetbarAdmin.vue'),
                    meta: {
                        hide: false, title: '商家设置', icon: 'ui-ico-hotel', roles:[1,2,3]
                    }
                },
                {
                    path:'/admin/accounts', component:()=>import('~/views/admin/AccountsDefault.vue'),
                    meta:{
                        hide:false, title:'角色管理', icon:'ui-ico-account', 
                        roles:[1, 2, 3], prod:['base','pro','super']
                    }
                }
            ]
        },{
            path: '/login', component: () => import('~/uiframe/layout/UiLogin.vue'),
            meta: { hide: true,title: '用户登录', open: true, prod: ['guest'] }
        }
    ]
});

router.meta = {
    activeMenu: ref('/')
}

const isLogined = () =>  Session.logined.value;

router.beforeEach((to, from, next) => {
    const m = to.meta;

    if (!m) return next();
    const au = Session.auth.value
    if (!isLogined() ) {
        if(to.path !== '/login')
            return next('/login');
    } else {
        
        if (m.roles && -1 == m.roles.indexOf(au.role_id)) {
            console.log('没有权限:', to.path)
            return next('/error/nopermission');
        }

        if (m.prod && -1 == m.prod.indexOf(au.prod)) {
            console.log('需要购买高级版本:', to.path)
            return next('/error/buy');
        }
    }
    return next();

})

router.afterEach(async (to) => {
    let mchs = to.matched;
    if (mchs.length > 0) {
        for (var i = mchs.length - 1; i >= 0; i--) {
            let m = mchs[i];
            if (m.meta && m.meta.hide === false) {
                router.meta.activeMenu.value = m.path;
                break;
            }
        }
    };
})


export default router
